import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router, ActivationEnd, RouterState, NavigationStart, NavigationExtras } from "@angular/router";
import { map, filter, debounceTime, distinctUntilChanged, switchMap, tap } from 'rxjs/operators';
import { AuthService } from '../..';
import { environment } from 'environments/environment';
import { SignalRService } from '../../core/service/singnalr-service';
import { NzModalService } from 'ng-zorro-antd';
import { HttpService } from "../../core/service/http.service";
@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
export class IndexComponent implements OnInit {
  userToggle = false;
  table;
  arPath;
  userRoleType = -1;
  userName = '';
  userPhoto = '';
  userId = 0;
  connectTimes = 0;
  isFeedback = false;
  isFeedbackdata; // 反馈信息数据

  get messages() {
    return this.signalrService.GetMessage();
  }

  constructor(
    private signalrService: SignalRService,
    private route: ActivatedRoute,
    private router: Router,
    public http: HttpService,
    private madol: NzModalService,
    private auth: AuthService) {
    this.signalrService.connectionEstablished.subscribe(e => {
      if (!e) {
        this.madol.confirm({
          nzTitle: '连接错误',
          nzContent: '无法连接信息服务，请重新登陆！',
          nzOnOk: () => {
            this.router.navigate(['/login']);
          },
          nzOnCancel: () => {

          }
        });
        throw new Error('连接超时');
        // if (this.connectTimes > 20) {
        //   this.madol.confirm({
        //     nzTitle: '连接错误',
        //     nzContent: '无法连接信息服务，请重新登陆！',
        //     nzOnOk: () => {
        //       this.router.navigate(['/login']);
        //     },
        //     nzOnCancel: () => {

        //     }
        //   });
        //   throw new Error('连接超时');
        // } else {
        //   this.signalrService.startConnection();
        //   this.connectTimes++;
        // }
      }
    });

    this.signalrService.goToDrill.subscribe(e => {
      this.madol.confirm({
        nzTitle: '演练提醒',
        nzContent: '您有一个演练步骤要操作，是否要前往？',
        nzOnOk: () => {
          const navigationExtras: NavigationExtras = {
            queryParams: { 'id': e }
          };
          this.router.navigate(['/mydrillcontent'], navigationExtras);
        },
        nzOnCancel: () => {

        }
      });
    });
    this.iniContent();
  }

  ngOnInit() {

    // if (this.userType === 1) {
    //   this.router.navigate(['app/studentindex/studentadd']);
    // }
  }


  iniContent() {
    this.router.events.pipe(
      filter(e => {
        return e instanceof NavigationStart;
      }),
    ).subscribe(e => {
      this.arPath = null;
    });
    this.router.events.pipe(
      filter(e => {
        return e instanceof ActivationEnd && (<ActivationEnd>e).snapshot.data['caption'] !== undefined;
      }),
      map(e => (<ActivationEnd>e).snapshot.data['caption']),
    ).subscribe(e => {
      if (this.arPath) {
        this.arPath.unshift(e);
      }
    });
    this.userRoleType = this.auth.token.userRoleType;
    this.userName = this.auth.token.userName;
    this.userPhoto = this.auth.token.photoUrl;
  }

  logout() {
    try {
      localStorage.setItem('authorization', JSON.stringify({}));
      window.location.reload();
    } catch (error) {
      console.log(error);
    }

  }

}
